export function transFormData(data) {
  const temp = JSON.parse(JSON.stringify(data))
  transForm(temp)
  return temp
}

function transForm(data) {
  for (const key of Object.keys(data)) {
    if (Object.prototype.toString.call(data[key]) === '[object Object]') {
      transForm(data[key])
    }
    if (data[key] === '' || (data[key] instanceof Array && data[key].length === 0)) {
      delete data[key]
    }
  }
}

export function convert(temp) {
  const list = JSON.parse(JSON.stringify(temp))
  const res = []
  const map = list.reduce((res, v) => {
    res[v.id] = v
    return res
  }, {})
  for (const item of list) {
    if (item.parentId === 0) {
      res.push(item)
      continue
    }
    if (item.parentId in map) {
      const parent = map[item.parentId]
      parent.children = parent.children || []
      parent.children.push(item)
    }
  }
  return res
}
